﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using SalesSys.Comm.DAL;
using SalesSys.DAL.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.SqlServer;
using System.Dynamic;
using System.Data.Entity.Infrastructure;

namespace SalesSys.DAL
{

    public interface IWXUserRepository : IRepository<Employee>
    {
        int GetUserCount();
    }

    public interface IDepotHistroyRep : IRepository<DepotHistroy>
    {
        DataTable GetTableInfo(string tableSql);
        IEnumerable<DepotHistroy> GetDepotHistroy(Guid depotId, DateTime dt);
    }
    [PartCreationPolicy(CreationPolicy.NonShared)]
    [Export(typeof(IWXUserRepository))]
    public class WXUserRepository : WXEFRepository<Employee>, IWXUserRepository
    {


        public int GetUserCount()
        {


            var count = ExeSqlCmd(SqlTextList.SqlList.WX_UserCount.CreateParams("IsLeave", false).AddParam("Name", "名称"));
            return Entities.Count(p => p.IDCard != null);

        }

        public DepotCommodity[] GetDepotHistroy(DateTime dt)
        {
            ExeSqlCmd(SqlTextList.SqlList.DeportHistory.CreateParams("time", dt));
            return null;
        }
    }






    [PartCreationPolicy(CreationPolicy.NonShared)]
    [Export(typeof(IDepotHistroyRep))]
    public class DepotHistroyRep : WXEFRepository<DepotHistroy>, IDepotHistroyRep
    {



        public IEnumerable<DepotHistroy> GetDepotHistroy(Guid depotId, DateTime dt)
        {
            var cmdRep = new WXEFRepository<Commodity>(); //SqlFunctions.GetDate()   //DbFunctions.
            cmdRep.UnitContext=this.UnitContext;
            var deps = cmdRep.Entities;
            var re = ExeSqlQuery<DepotHistroy>(SqlTextList.SqlList.DeportHistory.CreateParams("time", dt).AddParam("depotId", depotId));

            var list = from a in re join b in deps on a.CommodityId equals b.Id select new { his = a, commd = b };
            var data = list.Select(p => new DepotHistroy() { Commodity = p.commd, Amount = p.his.Amount, CommodityId = p.his.CommodityId, DepotId = p.his.DepotId, Price = p.his.Price, UpdateTime = p.his.UpdateTime, Id = p.his.Id });
            return data;

        }

        public DataTable  GetTableInfo(string tableSql)
        {

          //var li=  base.ExeSqlQuery(typeof (DbRawSqlQuery), (SqlText) ("select * from " + tableName));
          //  Console.WriteLine(li);
            var re = base.ExeSqlQuery(tableSql);
 
            return re;

        }

        public void List(DateTime begin,DateTime end)
        {
            //查询时间范围出库单
            //获取最后剩余库存，获取总出库量
            //按时间排序，查询小于时间范围的剩余库存范围
            //时间排序查询出库对应入库单
            //迭代出库单，计算对应入库数量
           var cmdRep = new WXEFRepository<DepotRecord>();
            var outList=cmdRep.Entities.Where(p => p.CreateTime >= begin && p.CreateTime <= end && p.Type == 0).ToArray();
           var outSum= outList.GroupBy(p => new {p.ProviderId, p.CommodityId})
                .Select(p => new {a=p.OrderByDescending(x => x.CreateTime).First(),b=p.Sum(x=>x.TotalWeight)}).ToArray();

        }

    }
}

    
